package com.cjb.cloudsystem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjb.cloudsecurity.userMsg.entity.SecurityUser;
import com.cjb.cloudsecurity.userMsg.entity.UserDetail;
import com.cjb.cloudsystem.entity.SysMenu;
import com.cjb.cloudsystem.enums.MenuTypeEnum;
import com.cjb.cloudsystem.service.SysMenuService;
import com.cjb.cloudsystem.vo.SysMenuVO;
import com.cjb.common.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @Author 1.0.0 2023-09-23
 * @Package IntelliJ IDEA
 * @Class SysMenuControl
 * @Date 2023-09-23
 */
@RestController
@Tag(name = "sys/menu")
@RequestMapping(value = {"sys/menu"})
@ApiResponses(@ApiResponse(responseCode = "200", description = "接口请求成功"))
@Slf4j
public class SysMenuController {
    /*
     * 注入事物类
     */
    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 单个保存
     *
     * @Author 1.0.0 2023-09-23
     * @Value
     * @Title
     */
    @PostMapping("/save")
    @Operation(summary = "单个保存")
    public Result<Boolean> save(@RequestBody SysMenu sysMenu) {
        return Result.success(sysMenuService.save(sysMenu));
    }

    /**
     * 批量保存
     *
     * @Author 1.0.0 2023-09-23
     * @Value
     * @Title
     */
    @PostMapping("/listSave")
    @Operation(summary = "批量保存")
    public Result<Integer> listSave(@RequestBody List<SysMenu> sysMenu) {
        return Result.success(sysMenuService.listSave(sysMenu));
    }

    /**
     * 单个查询
     *
     * @Author 1.0.0 2023-09-23
     * @Value
     * @Title
     */
    @GetMapping("{id}")
    @Operation(summary = "单个查询-通过ID")
    public Result<SysMenu> queryById(@PathVariable("id") Long id) {
        return Result.success(sysMenuService.queryById(id));
    }

    /**
     * 分页查询
     *
     * @Author 1.0.0 2023-09-23
     * @Value
     * @Title
     */
    @GetMapping("/queryPage")
    @Operation(summary = "分页查询-通过页码")
    public Result<Page<SysMenu>> queryPage(@RequestBody SysMenu sysMenu) {
        Page sysMenuPage = new Page<SysMenu>();
        return Result.success(sysMenuService.queryPage(sysMenuPage, sysMenu));
    }

    /**
     * 批量更新
     *
     * @Author 1.0.0 2023-09-23
     * @Value
     * @Title
     */
    @PostMapping("/listUpdate")
    @Operation(summary = "批量更新")
    public Result<String> listUpdate(@RequestBody List<SysMenu> sysMenu) {
        sysMenuService.listUpdate(sysMenu);
        return Result.success();
    }


    /**
     * 批量删除
     *
     * @Author 1.0.0 2023-09-23
     * @Value
     * @Title
     */
    @PostMapping("/listDelete")
    @Operation(summary = "批量删除")
    public Result<String> listDelete(@RequestBody List<SysMenu> sysMenu) {
        sysMenuService.listDelete(sysMenu);
        return Result.success();
    }

    /**
     * 通过id删除
     *
     * @Author 1.0.0 2023-09-23
     * @Value
     * @Title
     */
    @DeleteMapping("{id}")
    @Operation(summary = "单个删除-通过ID")
    public Result<String> deleteById(@PathVariable("id") Long id) {
        sysMenuService.deleteById(id);
        return Result.success();
    }

    /**
     * 返回全部信息
     *
     * @Author 1.0.0 2023-09-23
     * @Value
     * @Title
     */
    @GetMapping("/list")
    @Operation(summary = "返回所有信息")
    public Result<List<SysMenuVO>> list() throws Throwable {
        return Result.success(sysMenuService.returnAllData());
    }

    /**
     * 创建用户方法
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @GetMapping("/authority")
    @Operation(summary = "用户权限标识")
    public Result<Set<String>> authority() {
        UserDetail user = SecurityUser.getUser();
        Set<String> set = sysMenuService.getUserAuthority(user);
        return Result.success(set);
    }

    /**
     * 菜单导航
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @GetMapping("nav")
    @Operation(summary = "菜单导航")
    public Result<List<SysMenuVO>> nav() {
        UserDetail user = SecurityUser.getUser();
        List<SysMenuVO> list = sysMenuService.getUserMenuList(user, MenuTypeEnum.MENU.getValue());

        return Result.success(list);
    }
}
